package com.xmy.study.handler;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

public class MyMetaObjectHandler implements MetaObjectHandler {

    /*
    //插入填充
    @Override
    public void insertFill(MetaObject metaObject) {
        Object password = getFieldValByName("password", metaObject);
        if (ObjectUtils.isNull(password)) {
            setFieldValByName("password", "88888888", metaObject);
        }
    }*/

    @Override
    public void insertFill(MetaObject metaObject) {

        Long createdId = null;
        /*Subject subject = SecurityUtils.getSubject();
        //1.subject获取所有的安全数据集合
        PrincipalCollection principals = subject.getPrincipals();
        if(principals != null && !principals.isEmpty()){
            //2.获取安全数据
            Admin admin = (Admin) principals.getPrimaryPrincipal();
            createdId = admin.getId();
        }*/
        //根据名称设置属性值
        LocalDateTime nowLocalDateTime = LocalDateTime.now().withNano(0);
        this.setFieldValByName("createdAt",nowLocalDateTime,metaObject);
        this.setFieldValByName("createdId",createdId,metaObject);
        //this.setFieldValByName("updatedAt",DateUtil.getDateTime(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //根据名称设置属性值
        LocalDateTime nowLocalDateTime = LocalDateTime.now().withNano(0);
        this.setFieldValByName("updatedAt",nowLocalDateTime,metaObject);
    }

}
